#!/usr/bin/env python

import gtk
import gtk.glade
import os
import pango
import gobject

import uspCoreUtils 
import utils.GConfigurator as uspGConfig

from xdg.DesktopEntry import DesktopEntry

class SysInfoView(object):
	def __init__(self, widgetTree, mainWindow, mainEventWindow):
		mainVBox = widgetTree.get_widget("MainVBox1")
		
		hBox = gtk.HBox()
		loadLabel = self.createLabelWithText("Load")
		hBox.pack_start(loadLabel, False, False, 0)
		self.loadEntry = gtk.Entry()
		self.loadEntry.set_editable(False)
		hBox.pack_start(self.loadEntry, True, True, 0)
		
		mainVBox.pack_start(hBox, False, False, 0)
		
		hBox = gtk.HBox()
		uptimeLabel = self.createLabelWithText("Uptime")
		hBox.pack_start(uptimeLabel, False, False, 0)
		self.uptimeEntry = gtk.Entry()
		self.uptimeEntry.set_editable(False)
		hBox.pack_start(self.uptimeEntry, True, True, 0)
		
		mainVBox.pack_start(hBox, False, False, 0)
		
		hBox = gtk.HBox()
		uptimeIdleLabel = self.createLabelWithText("Idle")
		hBox.pack_start(uptimeIdleLabel, False, False, 0)
		self.uptimeIdleEntry = gtk.Entry()
		self.uptimeIdleEntry.set_editable(False)
		hBox.pack_start(self.uptimeIdleEntry, True, True, 0)
		
		mainVBox.pack_start(hBox, False, False, 0)
		
		self.timer_id = gobject.timeout_add(2000, self.update)
	
	def update(self):
		self.updateLoad()
		self.updateUptime()
		self.timer_id = gobject.timeout_add(2000, self.update)
		
	def updateLoad(self):
		one, five, fifteen = self.readLoad()
		loadText = one+" "+five+" "+fifteen
		self.loadEntry.set_width_chars(len(loadText))
		self.loadEntry.set_text(loadText)
	
	def updateUptime(self):
		uptime, uptimeIdle = self.readUptime()
		
		days, hours, minutes, seconds = self.splitTime(uptime)
		uptimeText = "%d Days %.2d:%.2d:%.2d" % (days, hours, minutes, seconds)
		self.uptimeEntry.set_width_chars(len(uptimeText))
		self.uptimeEntry.set_text(uptimeText)
		
		days, hours, minutes, seconds = self.splitTime(uptimeIdle)
		uptimeText = "%d Days %.2d:%.2d:%.2d" % (days, hours, minutes, seconds)
		self.uptimeIdleEntry.set_width_chars(len(uptimeText))
		self.uptimeIdleEntry.set_text(uptimeText)
	
	def splitTime(self, seconds):
		days = seconds // (60*60*24)
		seconds = seconds % (60*60*24)
		hours = seconds // (60*60)
		seconds = seconds % (60*60)
		minutes = seconds // 60
		seconds = seconds % 60
		return days, hours, minutes, seconds

	def readLoad(self):
		file = open("/proc/loadavg", "r")
		loadAvgRaw = file.read()
		file.close()
		
		loadAvgList = loadAvgRaw.strip().split()

		return loadAvgList[0], loadAvgList[1], loadAvgList[2]
		
	def readUptime(self):
		file = open("/proc/uptime", "r")
		uptimeRaw = file.read()
		file.close()

		uptimeList = uptimeRaw.strip().split()
		uptime = float(uptimeList[0])
		uptimeIdle = float(uptimeList[1])

		return int(uptime), int(uptimeIdle)

	def createLabelWithText(self, text):
		label = gtk.Label(text)
		label.set_width_chars(8)
		label.set_ellipsize(pango.ELLIPSIZE_END)
		itemStyle = pango.AttrList()
		itemStyle.insert(pango.AttrWeight(pango.WEIGHT_NORMAL, 0, -1))
		itemStyle.insert(pango.AttrSize(10*1000,0,-1))
		label.set_attributes(itemStyle)
		label.set_alignment(0.0, 0.5)
		return label

class Configuration(uspCoreUtils.Singleton):
	def __init__(self):
		uspCoreUtils.Singleton.__init__(self)
		
class UspPlugin(object):
	
	gladeFilename = "uspSysInfo.glade"
	
	def __init__(self):
		self.gladefile = os.path.join(os.path.dirname(__file__) , self.gladeFilename)
		
		if not os.path.exists(self.gladefile):
			raise Exception("Glade file '"+ self.gladeFilename +"' does not exist.")
			
		self.widgetTree = gtk.glade.XML(self.gladefile, "MainWindow")
		self.window = self.widgetTree.get_widget("MainWindow")
		self.contentHolder = self.widgetTree.get_widget("MainEventBox")
		
		self.gClient = uspGConfig.GConfigurator('/apps/usp2/plugins/sysinfo')
		Configuration().gClient = self.gClient
		
		self.userView = SysInfoView(self.widgetTree, self.window, self.contentHolder)
	
	def pluginName(self):
		return "System info"
	
	def mainWidget(self):
		return self.contentHolder
	
	def pluginIcon(self):
		return 'gnome-system'
